package spring.chatroom.java_chatroom.server;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import spring.chatroom.java_chatroom.mapper.FriendTableMapper;
import spring.chatroom.java_chatroom.common.utils.WaitToSendUtil;
import spring.chatroom.java_chatroom.common.utils.WebSocketSessionManagerUtil;
import spring.chatroom.java_chatroom.pojo.model.addFriendInfo;
import spring.chatroom.java_chatroom.pojo.model.msgInfo;
import spring.chatroom.java_chatroom.pojo.response.response;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class MesSendServer {

    @Autowired
    private FriendTableMapper friendTableMapper;

    public void sendMsg(msgInfo c) throws IOException {
        //根据SessionId来获取到userId
        List<Integer> ids = friendTableMapper.getFriendTableBySessionId(c.getSessionId());

        log.info("会话的俩个人的id = {}", ids);

        log.info("map = {}", WebSocketSessionManagerUtil.getMap());

        for (Integer id : ids) {
            //先获取到会话的session对象 一个WebSocketSession就表示的是一个服务器和客户端的连接
            WebSocketSession web = WebSocketSessionManagerUtil.getAttribute(id);

            //为空说明没有登录 为0就表示是somebody 那就不用在发了
            if (web == null) {
                if (id == 0) {
                    //说明没有登录 借用好友的WebSocketSession 来发送未登录的信息
                    msgInfo msg = new msgInfo();
                    BeanUtils.copyProperties(c, msg);
                    msg.setUserId(0);
                    msg.setContent("某人不在哦，你可以call管理员叫醒我哦 ε٩(๑> ₃ <)۶з");
                    msg.setUserName("某人");
                    web = WebSocketSessionManagerUtil.getAttribute(c.getUserId());

                    web.sendMessage(contentToTextMessage(msg));
                }
                continue;
            }


            log.info("sendMsg:{}", c);

            web.sendMessage(contentToTextMessage(c));
        }
    }

    //发送添加朋友请求
    public void tranFriendReq(Integer targetId,addFriendInfo content) throws IOException {
        //获取到targetId的webSocket
        WebSocketSession attribute = WebSocketSessionManagerUtil.getAttribute(targetId);
        if (attribute == null) {
            //todo 为空就先把这个请求给储存下
            log.info("method{tranFriendReq}: 对方未登录 user_id={}", targetId);
            WaitToSendUtil.setAttribute(targetId, content);
            return;
        }

        attribute.sendMessage(contentToTextMessage(content));
    }

    //告诉friendId
    public void addFriendAgree(Integer friendId) throws IOException {
        WebSocketSession attribute = WebSocketSessionManagerUtil.getAttribute(friendId);

        if (attribute == null) {
            WaitToSendUtil.setAttribute(friendId, Map.of("type", "addFriendAgree"));
            log.info("未登录 已登记事件 userId = {} type = {}",friendId,"addFriendAgree");
            return;
        }

        attribute.sendMessage(contentToTextMessage(Map.of("type","addFriendAgree")));
    }

    private TextMessage contentToTextMessage(Object content) {

        String json = JSONUtil.toJsonStr(response.success(content));

         return new TextMessage(json);
    }
}
